package com.asia5b.a5mall.app_ui.activity

import android.Manifest
import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.databinding.ActivitySplashBinding
import com.asia5b.a5mall.middleware.api.TokenManager
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.utils.joinComposite
import com.asia5b.a5mall.middleware.utils.push
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import java.util.concurrent.TimeUnit


/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   欢迎页面
 */
@Route(path = ActCons.SplashActivity)
class SplashActivity : SimpleActivity<SimpleViewModel, ActivitySplashBinding>() {

    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun initView() {
        //-----------增加这段代码是为了解决release安装包安装后打开app后再按home键再次进入SplashActivity的BUG----------start
        if (!this.isTaskRoot) {
            val intent = intent
            if (intent != null) {
                val action = intent.action
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
                    finish()
                    return
                }
            }
        }
        requestPermissions()
//        presenter.requestConfiguration(this)

    }

    private fun requestPermissions() {


        RxPermissions(this)
                .request(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    toHome()
                }, Consumer {
                    toHome()
                })
    }

    fun toHome() {
        if (!UserRepository.isLogin()) {
            TokenManager(this).clearToken()
            TokenManager(this).removeUid()
        }
        push(ActCons.MainActivity)
        Observable.timer(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    finish()
                }.joinComposite(mCompositeDisposable)
    }

    override fun initEvent() {

    }

    override fun initData() {

    }

    /**
     * 重写返回监听事件 直接销毁页面
     */
    override fun onBackPressed() {
        finish()
    }


//    override fun requestConfigurationResult() {
//        if (CountrysRepository.allIsNull()) {//手动配置默认信息
//            val temp = CountryAllModel()
//            val countryModel = CountryModel().run {
//                country_id = 12
//                country_name = "中国（China）"
//                country_abbreviation = "cn"
//                country_en_name = "China"
//                country_code = "86"
//                lang_name = "China"
//                this
//            }
//            temp.country_list= arrayListOf(countryModel)
//            val currencyModel=CurrencyModel().run {
//                currency_id= 7
//                currency_name= "人民币"
//                currency_short="rmb"
//                country_id= 12
//                currency_mark= "¥"
//                this
//            }
//            temp.currency_list= arrayListOf(currencyModel)
//            CountrysRepository.countryAllModel=temp
//        }
//        toHome()
//    }
}